﻿/************************************************************************************
 * xujiajun@yoozoo.com 御剑
 * 层级抽象类
 ***********************************************************************************/

using Unity.Mathematics;

namespace WorldMapBase.Layer
{
    /// <summary>
    /// 层级抽象类
    /// </summary>
    public abstract class Layer
    {
        /// <summary>
        /// 下一帧是否强行刷新视图
        /// </summary>
        public bool forceUpdateViewRect;

        protected internal int layerID;
        
        /// <summary>
        /// 层级ID
        /// </summary>
        protected internal int id;

        /// <summary>
        /// 是否执行了创建的标记
        /// </summary>
        private bool b_Created;

        /// <summary>
        /// 是否已经创建
        /// </summary>
        /// <returns></returns>
        public bool IsCreated()
        {
            return b_Created;
        }

        /// <summary>
        /// 内部调用。销毁层级
        /// </summary>
        internal void Destroy()
        {
            if (b_Created)
            {
                OnDestroy();
                b_Created = false;
            }
        }

        /// <summary>
        /// 内部调用。创建层级
        /// </summary>
        internal void Create()
        {
            if (!b_Created)
            {
                b_Created = true;
                OnCreate();
            }
        }

        /// <summary>
        /// 内部调用。更新视图
        /// </summary>
        /// <param name="viewRect">视图尺寸（xy：左下世界坐标，zw：长宽）</param>
        internal void UpdateViewRect(int4 viewRect)
        {
            if (b_Created)
            {
                OnUpdateViewRect(viewRect);
            }
        }

        /// <summary>
        /// 创建层级时响应
        /// </summary>
        protected abstract void OnCreate();

        /// <summary>
        /// 销毁层级时响应
        /// </summary>
        protected abstract void OnDestroy();

        /// <summary>
        /// 更新视图时响应
        /// </summary>
        /// <param name="viewRect">视图尺寸（xy：左下世界坐标，zw：长宽）</param>
        protected abstract void OnUpdateViewRect(int4 viewRect);
    }

}
